package au.org.nh.dao.mongo.impl;

import java.net.UnknownHostException;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.MongoURI;

/* This class is a wrapper around the Mongo DB API, It assumes that the caller wants to talk
 * to just one DB. It does not support connections to multiple DB's. However, it may support 
 * this in the future. It also assumes the db parameters comes from the servlet context hence is
 * tightly coupled to the servlet container. 
 * TODO: Break the dependency by using an IoC container 
 */

public class MongoDBUtil {
	private static String dbName;
	private static String dbHost;
	private static String dbPort;
	private static String dbUserName;
	private static String dbPassword;
	private static MongoURI mongoDbURI = null;
	private static String dbURI = null;
	private static Mongo m = null;
	private static DB db = null;

	// static {
	// try {
	// dbName = ServletContextAppListener
	// .getProperty(ConfigParams.DB_NAME);
	// dbHost = ServletContextAppListener
	// .getProperty(ConfigParams.DB_HOST);
	// dbPort = ServletContextAppListener
	// .getProperty(ConfigParams.DB_PORT);
	// dbUserName = ServletContextAppListener
	// .getProperty(ConfigParams.DB_USERNAME);
	// dbPassword = ServletContextAppListener
	// .getProperty(ConfigParams.DB_PASSWORD);
	//
	// /* Build the URI based on these values */
	// dbURI = buildURI();
	// System.out.println("DB URI is " + dbURI);
	// // mongoDbURI = new MongoURI(dbURI);
	// db = getDB(dbName);
	// db.authenticate(dbUserName, dbPassword.toCharArray());
	// } catch (final LookedUpPropertyNotFoundException e) {
	// e.printStackTrace();
	// } catch (final MongoException e) {
	// e.printStackTrace();
	// } catch (final MongoDBException e) {
	// e.printStackTrace();
	// }
	// }

	private static DB getDB(final String dbName) throws MongoDBException {
		if (db != null) {
			return db;
		}

		try {
			// m = new Mongo(mongoDbURI);
			m = new Mongo(dbURI);
		} catch (final UnknownHostException e) {
			e.printStackTrace();
			throw new MongoDBException(e.getMessage());
		} catch (final MongoException e) {
			e.printStackTrace();
			throw new MongoDBException(e.getMessage());
		}
		db = m.getDB(dbName);
		return db;
	}

	public static DBCollection getCollection(final String collName)
			throws MongoDBException {
		final DBCollection coll = db.getCollection(collName);
		if (coll == null) {
			throw new MongoDBException("Could not find collection");
		}
		return coll;
	}

	// private static String buildURI() {
	// /*
	// * URI looks like
	// * mongodb://[username:password@]host1[:port1][,host2[:port2],...
	// * [,hostN[:portN]]][/[database][?options]] from
	// * http://api.mongodb.org/java/2.8.0/
	// */
	// final StringBuffer URIBuffer = new StringBuffer();
	// return URIBuffer.append("mongodb://").append(dbUserName).append(":")
	// .append(dbPassword).append("@").append(dbHost).append(":")
	// .append(dbPort).append("/").append(dbName).toString();
	// }

	private static String buildURI() {
		/*
		 * URI looks like
		 * mongodb://[username:password@]host1[:port1][,host2[:port2],...
		 * [,hostN[:portN]]][/[database][?options]] from
		 * http://api.mongodb.org/java/2.8.0/
		 */
		final StringBuffer URIBuffer = new StringBuffer();
		return URIBuffer.append(dbHost).append(":").append(dbPort).toString();
	}

}
